﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CrankShaftAnalysisSystem.Controller
{
    public class LogicalController
    {
        private static readonly LogicalController instance = new LogicalController();

        /// <summary>
        /// 显式的静态构造函数用来告诉C#编译器在其内容实例化之前不要标记其类型
        /// 静态构造函数是C#的一个新特性，其实好像很少用到。 
        /// 不过当我们想初始化一些静态变量的时候就需要用到它了。 这个构造函数是属于类的，
        /// 而不是属于哪里实例的，就是说这个构造函数只会被执行一次。 
        /// 也就是在创建第一个实例或引用任何静态成员之前，由.NET自动调用
        /// </summary>
        static LogicalController() { }
        public static LogicalController Instance
        {
            get
            {
                return instance;
            }
        }
        public MeasureSystem system;
        public System.Timers.Timer PollTimer { get; private set; } // 相对优先级低的定时器

        public bool Initialized { get; private set; } = false;
        public ushort ComminucationCnt { get; internal set; }

        private LogicalController()
        {
            system = MeasureSystem.Instance;
            PollTimer = new System.Timers.Timer();
            PollTimer.Interval = 200;
            PollTimer.AutoReset = true;
            PollTimer.Elapsed += PollTimer_Elapsed;
        }

        public static bool MT2571OutCmd { get; private set; } = false;
        public static bool MT2571BackCmd { get; private set; } = false;
        public static bool VacuumOnCmd { get; private set; } = false;
        public static bool VacuumOffCmd { get; private set; } = false;
        public static bool PenCompressReached { get; private set; }
        public static bool EncoderNearZero { get; private set; }

        public static void MT2571Out()
        {
            MT2571OutCmd = true;
            MT2571BackCmd = false;
        }

        public static void MT2571Back()
        {
            MT2571OutCmd = false;
            MT2571BackCmd = true;
        }

        public static void VacuumOn()
        {
            VacuumOnCmd = true;
            VacuumOffCmd = false;
        }

        public static void VacuumOff()
        {
            VacuumOnCmd = false;
            VacuumOffCmd = true;
        }

        private void PollTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            var msg = new ModbusProxy.Message();
            msg.Address = VirtualDevice.Address.PLCCommand;
            var bnear_zero = system.ERM2400.NearZero();
            system.MT2571.GetDistance(); //刷新下数值用于显示
            EncoderNearZero = bnear_zero;
            PenCompressReached = system.Easten.PenCompressionReached();
            ushort near_zero = Convert.ToUInt16(bnear_zero);
            ushort axis3_reached = Convert.ToUInt16(PenCompressReached);
            ushort mt2571out = Convert.ToUInt16(MT2571OutCmd);
            ushort mt2571back = Convert.ToUInt16(MT2571BackCmd);
            ushort vacuumon = Convert.ToUInt16(VacuumOnCmd);
            ushort vacuumoff = Convert.ToUInt16(VacuumOffCmd);
            ComminucationCnt++;
            if(ComminucationCnt > 1000)
            {
                ComminucationCnt = 1;
            }
            ushort[] regs = new ushort[] { near_zero, axis3_reached, mt2571out, mt2571back, vacuumon, vacuumoff, ComminucationCnt};
            msg.Regs = regs;
            system.PLC.Device.SendRegistersAsync(msg);
        }

        public void Begin()
        {
            if (Initialized) return;
            Initialized = true;
            PollTimer.Start();
        }
    }
}
